/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef HPSC_H
#define HPSC_H
#include <QtDebug>
#include <stdio.h>
#include <QObject>
#include <QtNetwork/QTcpSocket>
#include <QString>
#include <QTimer>

#include "qextserialport.h"
#include "hpsccommadthread.h"

#include "gssglobals.h"

#define CR 0x0D
/**
"HEIDEN" Power Supply Control class

	@author Archimedes Projekt <thelsb@gmail.com>
*/


class CHPSC : public QObject
{
Q_OBJECT

public:
	enum STATUS { QUE=0x8,MAV=0x10,ESB=0x20,RQS=0x40 };		
	enum EVENT { QUERY_ERR=0x4,SRC_NO_RES=0x8,EXE_ERR=0x10,SYN_ERR=0x20,PON=0x80};
	enum SRC_ERR { V_ERR=0x01,A_ERR=0x02,POW_ERR=0x04,TEMP_ERR=0x08,ERR_SRC1=0x10,ERR_SRC2=0x20,ERR_SRC3=0x40,ERR_SRC4=0x80 };
	enum SRC { SRC1=1,SRC2,SRC3,SRC4,NOSRC}; 
    enum QUERY { V1,A1,V2,A2,V3,A3,V4,A4,READ_STAT,READ_CHANS };
    
	
	
	protected:
    	QTcpSocket *m_socket;
	QString m_serPort;
	QextSerialPort *m_serial;
	SRC m_currSrc;
	QTimer *m_pollTimer;
	quint16 m_hpscErrReg;
	HPSCCommandThread *m_commandLoop;
	QString m_readout,
			m_readoutV1,
			m_readoutV2,
			m_readoutV3,
			m_readoutV4,
			m_readoutA1,
			m_readoutA2,
			m_readoutA3,
			m_readoutA4;
    QUERY m_query;

private:

public:
	CHPSC(QString serPort,QObject *parent=NULL);
	//HPSC(quint16 guiPort=0,QString guiServer="localhost",QObject *parent = 0);

    ~CHPSC();

    
    /*!
        \fn HPSC::setV(float v,SRC src)
     */
    inline void setV(float v,SRC src)
    {
		QString var = QString::number(v);
		QString val = QString::number(v,'g',2);
		switch(src)
		{
			case SRC1: sendCommand(QString("VOLT1 ")+val); 
			//m_commandLoop->execute();
				break;
			case SRC2: sendCommand(QString("VOLT2 ")+val); 
			//m_commandLoop->execute();
				break;
			case SRC3: sendCommand(QString("VOLT3 ")+val); 
			//m_commandLoop->execute();
				break;
			case SRC4: sendCommand(QString("VOLT4 ")+val); 
			//m_commandLoop->execute();
				break;
            default: break;
		}
		
    }

    /*!
        \fn HPSC::setA(float a,SRC src)
     */
    inline void setA(float a,SRC src)
    {
		QString var = QString::number(a);
		int p;
		a < 0 ? p=4 : p=2; 
		QString val = QString::number(a,'g',2);
		switch(src)
		{
			case SRC1:sendCommand(QString("CURR1 ")+val,50000); 
				break;
			case SRC2:sendCommand(QString("CURR2 ")+val,50000);
				break;
			case SRC3:sendCommand(QString("CURR3 ")+val,50000);
				break;
			case SRC4:sendCommand(QString("CURR4 ")+val,50000);
				break;
            default: break;
		}
    }

    /*!
        \fn HPSC::setCurrentSrc(SRC src)
     */
    inline void setCurrentSrc(SRC src)
    {
        m_currSrc = src;
    }

   

    /*!
        \fn HPSC::setRangeMinV(SRC src)
     */
    inline void setRangeMinV(SRC src)
    {
		switch(src)
		{
			case SRC1:
				break;
			case SRC2:
				break;
			case SRC3:
				break;
			case SRC4:
				break;
                default: break;
		}
    }

    /*!
        \fn HPSC::setRangeMaxV(SRC src)
     */
    inline void setRangeMaxV(SRC src)
    {
		switch(src)
		{
			case SRC1:
				break;
			case SRC2:
				break;
			case SRC3:
				break;
			case SRC4:
				break;
            default: break;
		}
    }

    /*!
        \fn HPSC::setRangeMaxA(SRC src)
     */
    inline void setRangeMaxA(SRC src)
    {
		switch(src)
		{
			case SRC1:
				break;
			case SRC2:
				break;
			case SRC3:
				break;
			case SRC4:
				break;
                default: break;
		}
    }

    /*!
        \fn HPSC::setRangeMinA(SRC src)
     */
    inline void setRangeMinA(SRC src)
    {
        switch(src)
        {
            case SRC1:
            break;
            case SRC2:
            break;
            case SRC3:
            break;
            case SRC4:
            break;
            default: break;
        }
    }
    SRC_ERR getHPSCerror();
    STATUS getHPSCstatus();
    EVENT getHPSCevent();
    //void sendCommand(HPSC_Command command);
    void sendCommand(QString cmd,long usWait=30000);
    
    QString hpscReadoutV(int src);
	QString hpscReadoutA(int src);
    HPSCCommandThread * eventLoop();
	QString hpscReadout();

public slots:
    void pollStatus();
	void readHpscV(int src,QString);
	void readHpscA(int src,QString);
	void readHpsc(QString readout);
    
signals:
	void sig_hpscError(int,quint8);
    //sends V and A for SRC
    void sig_hpscReadout(int,float,float);
    void sig_hpscReadoutV(int,float);
    void sig_hpscReadoutA(int,float);
};

#endif

